package com.gzx.plugin.tjzy.modular.call.ht.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.call.ht.entity.GzxHtCallRecord;
import com.gzx.plugin.tjzy.modular.call.ht.entity.vo.GzxCallReportVo;
import com.gzx.plugin.tjzy.modular.call.ht.entity.vo.GzxHtCallRecordVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.common.mapper.BaseMapperPlus;

import java.util.List;

/**
 * @author zjt
 * @description
 * @since 2024/10/11 10:10
 */
public interface GzxHtCallRecordMapper extends BaseMapperPlus<GzxHtCallRecord, GzxHtCallRecordVo> {

    /**
     * 批量保存恒天话单
     *
     * @param htCallRecordList
     */
    @Insert("<script>" +
            "INSERT INTO gzx_ht_call_record (" +
            "business, call_type, call_id, agent_num, user_name, caller, callee, " +
            "ring_time, ring_duration, connect_time, start_time, end_time, call_duration, " +
            "file_url, area, result, `signal`, task_id, import_time, add_time, acd_end_time, " +
            "acd_duration, invite_comments, myd_key, asr_code, asr_name, company_name, " +
            "case_id,tenant_id,dept_id,created_time) " +
            "VALUES " +
            "<foreach collection='htCallRecordList' item='item' separator=','>" +
            "(#{item.business}, #{item.callType}, #{item.callId}, #{item.agentNum}, " +
            "#{item.userName}, #{item.caller}, #{item.callee}, " +
            "#{item.ringTime}, #{item.ringDuration}, #{item.connectTime}, " +
            "#{item.startTime}, #{item.endTime}, #{item.callDuration}, " +
            "#{item.fileUrl}, #{item.area}, #{item.result}, #{item.signal}, " +
            "#{item.taskId}, #{item.importTime}, #{item.addTime}, #{item.acdEndTime}, " +
            "#{item.acdDuration}, #{item.inviteComments}, #{item.mydKey}, " +
            "#{item.asrCode}, #{item.asrName}, #{item.companyName}, " +
            "#{item.caseId},#{item.tenantId},#{item.deptId},#{item.createdTime})" +
            "</foreach>" +
            "</script>")
    void insertBatch(@Param("htCallRecordList") List<GzxHtCallRecord> htCallRecordList);

    /**
     * 统计坐席工作报表
     *
     * @param page
     * @param createDate
     * @param deptId
     * @return
     */
    @Select("<script>" +
            "select agent_num as agentNum," +
            "count(if(call_duration > 0, 1, null)) as connectCalls," +
            "count(1) as totalCalls," +
            "sum(call_duration) as callDuration," +
            "count(if(call_duration >= 15, 1, null)) as validConnected," +
            "sum(if(call_duration >= 15, call_duration, null)) as validCallDuration " +
            "from gzx_ht_call_record force index (create_time_idx) " +
            "where created_time BETWEEN CONCAT(#{createDate}, ' 00:00:00') AND CONCAT(#{createDate}, ' 23:59:59') " +
            "and dept_id = #{deptId}" +
            "group by agent_num " +
            "</script>")
    Page<GzxCallReportVo> pageCallReport(@Param("page") Page<GzxCallReportVo> page, @Param("createDate") String createDate, String deptId);

    /**
     * 导出统计坐席工作报表
     *
     * @param date
     * @param userNames
     * @return
     */
    List<GzxCallReportVo> selectCallRecordsStatisticExport(@Param("date") String date, List<String> userNames);
}
